﻿/*----------------------------------------------------------------
           // Copyright (C) 2008 安华顺通(北京)科技有限公司
           // 版权所有。 
           //
           // 文件名：TablesContextMenu.cs
           // 文件功能描述：表的右键菜单。
           //
           // 
           // 创建标识：杨波
           //
           // 修改标识：杨波20080827
           // 修改描述：添加表时，加表的初始字段到SQL Server。
           //
           // 修改标识：
           // 修改描述：
//----------------------------------------------------------------*/

#region UsingStatements

using System;
using System.Data;
using System.IO;
using System.Text;
using System.Xml;
using RuleCode.compiler;

#endregion

namespace RuleCode.ObjectTree
{
    public partial class TablesContextMenu : CompileContextMenu
    {
        private string newName;

        public TablesContextMenu()
        {
            //base.InitializeComponent();
            InitializeComponent();
            /*this.toolStripMenuItem3 = new ToolStripMenuItem();
            this.baseContextMenuStrip.Items.Add(this.toolStripMenuItem3);
                  this.toolStripMenuItem3.Name = "toolStripMenuItem3";
            this.toolStripMenuItem3.Size = new System.Drawing.Size(178, 22);
          
            this.toolStripMenuItem3.Text = "新建表";

            this.toolStripMenuItem3.Click += new System.EventHandler(this.toolStripMenuItem3_Click);*/
        }


        private void CreateFile()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("#region using");
            sb.AppendLine("using System;");
            sb.AppendLine("using RuleCode.ObjectTree;");
            sb.AppendLine("using RuleCode.Table;");
            //sb.AppendLine("using RuleCode.Quries;");
            sb.AppendLine("using RuleCode.Common;");
            sb.AppendLine("using RuleCode.Enum;");
            sb.AppendLine("using RuleCode.ExtendType;");




            //sb.AppendLine("using RuleCode.CurrentUser;");
            //sb.AppendLine("using RuleCode.FrameWork;");
            //sb.AppendLine("using RuleCode.ObjectTree;");
            //sb.AppendLine("using RuleCode.Query;");
            //sb.AppendLine("using RuleCode.ObjectTree.Enum;");
            //sb.AppendLine("using RuleCode.ObjectTree.ExtendDataType;");
            //sb.AppendLine("using RuleCode.ObjectTree.Table;");
            //sb.AppendLine("using RuleCode.Query.AOTCommonClass;");
            //sb.AppendLine("using " + SysDefalutNamespace.DefaultEnumNamespace + ";");
            ////sb.AppendLine("using RuleCode.Query.Forms;");
            //sb.AppendLine("using RuleCode.Query.InheritControl;");
            ////sb.AppendLine("using RuleCode.Query.Language;");
            //sb.AppendLine("using RuleCode.Query.Queries;");
            //sb.AppendLine("using RuleCode.Query.SQL;");
            //sb.AppendLine("using RuleCode.Query.Tables;");
            //sb.AppendLine("using RuleCode.Query.Type;");
            sb.AppendLine("#endregion");

            sb.AppendLine("namespace " + SysDefalutNamespace.DefaultTableNameSpace);
            sb.AppendLine("{");
            sb.AppendLine("\tpublic partial class " + newName);
            sb.AppendLine("\t{");
            //sb.AppendLine("\t\tpublic  " + newName + "()");
            //sb.AppendLine("\t\t{");
            //sb.AppendLine("\t\t\t");
            //sb.AppendLine("\t\t\t");
            //sb.AppendLine("\t\t}");
            sb.AppendLine("\t}");
            sb.AppendLine("}");
            var dir = new DirectoryInfo(Util.GetTablesPath );// (Util.OTPath + @"\Module\Tables");
            if (!dir.Exists)
            {
                dir.Create();
            }
            StreamWriter swr =
                new StreamWriter(Util.GetTablesPath + "\\" + Util.SelectedNode.Text + ".cs", false,
                                 Encoding.UTF8);

            swr.Write(sb);

            swr.Flush();

            swr.Close();
        }

        /// <summary>
        /// 
        /// </summary>
        private void addTable()
        {
            // //XmlNode table = new xmlno
            // //XmlNode tableNode = Util.SysXmlDocument.SelectSingleNode("对象树//表");
            // //string path = (Util.ActiveTreeView.BaseNodePath+Util.SelectedNode.FullPath).Replace("\\","/");
            // //XmlNode tableNode = Util.SysXmlDocument.SelectSingleNode(path);
            // Util.SelectedNode.Expand();
            // XmlNode tableTemplateNode = TreeViewOpr.nodesTemplate.SelectSingleNode("//Tables");
            // XmlNode tablesNode = Util.getAddingParentNode();
            // XmlNode tableNode = Util.SysXmlDocument.CreateElement("newTable");
            // tableNode = tableTemplateNode.CloneNode(true).FirstChild;
            // tableNode = Util.SysXmlDocument.ImportNode(tableNode, true);
            // tablesNode.InsertAfter(tableNode, tablesNode.FirstChild);
            // //End insert to AOTFile
            // string ID = Util.getMaxTypeID(tableNode);
            // //SysTreeNode newTable = new SysTreeNode(tableNode.Name);
            // SysTreeNode newTable = new SysTreeNode(OT.Table);
            // newTable.SysXmlNode = tableNode;
            // Util.SelectedNode.Nodes.Insert(0, newTable);
            // TableProperty tableProperty = new TableProperty(ID);
            // tableProperty.Name = newTable.Text;
            // Util.SetPropertyContent(tableProperty);
            // newTable.Property = tableProperty;
            // tableProperty.SerializeToXML();
            // //End add table node
            // //Begin add table subnodes
            // foreach (XmlNode node in tableNode.ChildNodes)
            // {
            //     Util.addSubNode(node, newTable);
            // }

            // //
            // //string newName = Util.getInitName(Util.SelectedNode, OT.Table);
            // newName = OT.Table + ID;
            // newTable.Text = newName;
            // tableProperty.Name = newName;
            // newTable.SysXmlNode = Util.changeXmlNodeName(tableNode, newName);
            // Util.ActiveTreeView.SelectedNode = newTable;
            // Util.SelectedNode = newTable;



            // //tableEle.SetAttribute("ID", "3");
            // //tableEle.SetAttribute("Type", "Table");
            // //tableNode.InsertAfter(tableEle, tableNode.FirstChild);
            // //tableNode.AppendChild(tableEle);
            // //SysTreeNode newTable = new SysTreeNode("newTable");
            // //newTable.Tag = tableEle;
            // TreeViewOpr.saveToXML();
            // /*newTreeNode.Text = node.Name;
            // newTreeNode.ImageIndex = int.Parse(node.Attributes.GetNamedItem("ID").Value);
            // newTreeNode.SelectedImageIndex = int.Parse(node.Attributes.GetNamedItem("ID").Value);
            // newTreeNode.ToolTipText = "THIS A TOOLTIP";
            // newTreeNode.Name = node.Name;
            // newTreeNode.Tag = node;
            //*/
            // //Util.SelectedNode.Nodes.Add(newTable);
            // // Util.SelectedNode.Nodes.Insert(0, newTable);
            // Util.MdiParent.refreshAllOT();
            //TreeViewOpr.saveNodes();
            newName = Util.addNode(OT.XPathRootSeperator + OT.XPathRoot + OT.Tables, OT.Table,
                                       typeof(TableProperty), "");
            Util.SelectedNode = Util.SelectedNode.FindNode("FieldGroups");
            SysTreeNode fieldgroups = Util.SelectedNode;
           string  newName1 = Util.addNode(OT.XPathRootSeperator + OT.XPathRoot + OT.FieldGroups, OT.FieldGroup,
                                      typeof(TableFieldGroupProperty), "OverView");
           Util.SelectedNode = fieldgroups;
           newName1 = Util.addNode(OT.XPathRootSeperator + OT.XPathRoot + OT.FieldGroups, OT.FieldGroup,
                                      typeof(TableFieldGroupProperty), "AutoLookup");

            SqlHelper sqlHelp = SqlHelper.Instance;
            sqlHelp.ExecuteNonQuery(CommandType.Text,
                                    "CREATE TABLE [dbo].[" + newName +
                                    "]([DATAAREAID] [varchar](3) COLLATE Chinese_PRC_CI_AS NOT NULL DEFAULT (''),[RECID] [bigint] NOT NULL) ");
            sqlHelp.ExecuteNonQuery(CommandType.Text,
                                         "CREATE UNIQUE INDEX " + "Idx_" + newName + "_Recid" +
                            " ON [" + newName + "](RECID);");
       


        }


        private void createTableToolStripMenuItem_Click(object sender, EventArgs e)
        {
            addTable();
            CreateFile();
        }

        protected override void CompileNode()
        {
            base.CompileNode();
            SysCompiler.Instance.compileTables(Util.SelectedNode);
        }
    }
}